#include "Tree.h"
#include "PartitionElement.h"

using namespace std ;

//Counter of nodes
int Tree::counter = 0 ;

//Constructor for root
Tree::Tree( Sudoku* newSudoku ):sudoku(newSudoku), partitionElement(0)
{
    counter = 0 ;
    number = counter++ ;
}

//Constructor for not root nodes
Tree::Tree(PartitionElement* newPartition , Sudoku *newSudoku)
    :partitionElement(newPartition), sudoku(newSudoku)
{
    number = counter++ ;
    sudoku->initTransaction() ;
    partitionElement->exec() ;
}

int Tree::getCounter()
{
    return counter ;
}



bool Tree::start()
{
    //Set assumptions
    sudoku->service()   ;

    if( sudoku->allIsFilled() )
    {
        return true ;
    }

    if( sudoku->isDeadlock() )
    {
        sudoku->clearLastTransaction() ;
        return false ;
    }

    PartitionElement* partition = sudoku->makeAssumption() ;

    right = new Tree( partition, sudoku ) ;
    if( right->start() == true )
    {
        return true ;
    }
    delete right ;

    partition->radicallyChange() ;

    left = new Tree( partition, sudoku ) ;
    if( left->start() == true )
    {
        return true ;
    }
    delete left ;

    sudoku->clearLastTransaction() ;
    return false ;
}
